package com.bailiban.aigou.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.bailiban.aigou.constants.Constants;
import com.bailiban.aigou.entity.User;
import com.bailiban.aigou.service.IUserService;
import com.opensymphony.xwork2.ActionSupport;

@Controller
@Scope("prototype")
@Results({
	@Result(name = "error", location = Constants.SYS_JSP_BASEURL + "admin/500.jsp"),
	@Result(name = "login", location = Constants.SYS_JSP_BASEURL + "admin/login/login.jsp")
})
public class AdminLoginAction extends ActionSupport {
	
	@Autowired
	private IUserService userService;
	
	@Action(value = "", results = {
		@Result(name = SUCCESS, location = Constants.SYS_JSP_BASEURL + "admin/login/login.jsp")	
	})
	public String loginPage(){
		System.out.println("123");
		return SUCCESS;
	}
	
	@Action(value = "/admin/login", results = {
			@Result(name = SUCCESS, location = Constants.SYS_JSP_BASEURL + "admin/index/index.jsp")
	})
	public String login(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		User user = userService.getUserByName(username);
		if (user==null) {
			request.setAttribute("errMsg", "用户不存在");
			return LOGIN;
		}
		if (!user.getPassword().equals(password)) {
			request.setAttribute("errMsg", "密码错误");
			return LOGIN;
		}
		return SUCCESS;
	}
}
